package com.lbc.single;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//  enum(枚举) 是什么？ 本身也是一个 Class类
public enum EnumSingle {

    INSTANCE;

    public EnumSingle getInstance() {
        return INSTANCE;
    }

}

class Test {

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        EnumSingle instance1 = EnumSingle.INSTANCE;
//        EnumSingle instance2 = EnumSingle.INSTANCE;

        // NoSuchMethodException 意思是没有这个空参构造方法
        // Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(null);
        // IllegalArgumentException: Cannot reflectively create enum objects  不能用反射破坏枚举单例
        Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class,int.class);
        declaredConstructor.setAccessible(true);
        EnumSingle instance3 = declaredConstructor.newInstance();

        System.out.println(instance1);
//        System.out.println(instance2);
        System.out.println(instance3);
    }
}
